package com.chris.code.controller;

import cn.hutool.core.util.ZipUtil;
import com.chris.code.model.GenerateParams;
import com.chris.code.model.Table;
import com.chris.code.service.CodeService;
import com.chris.code.service.DatabaseService;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.List;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * <p>
 * Author: haoka (haokang207@126.com)
 * <p>
 * Date: 2018/9/30 10:11
 **/
@RestController
@RequestMapping("/code")
@Slf4j
public class CodeController {

    @Autowired
    private CodeService codeService;
    @Autowired
    private DatabaseService databaseService;

    @PostMapping
    public void generate(GenerateParams params, HttpServletResponse response) throws IOException, SQLException, ClassNotFoundException, TemplateException {
        Table table = null;
        List<Table> tables = databaseService.fetchTables(params.getDatabase());
        for (Table t : tables) {
            if (t.getName().equals(params.getTableName())) {
                table = t;
                break;
            }
        }
        codeService.generate(params, table, databaseService.fetchTableField(params.getDatabase(), table.getName()));
        //清理以前的压缩包
        String folderPath = new File("code").getAbsolutePath();
        String zipFileName = params.getName() + ".zip";
        File zipFile = new File(zipFileName);
        if (zipFile.exists()) {
            zipFile.delete();
        }
        ZipUtil.zip(new File(zipFile.getAbsolutePath()), true, new File(folderPath + "/java"), new File(folderPath + "/ui"));
        response.setContentType("application/x-zip-compressed");
        response.setHeader("Content-Disposition", "attachment;filename=" + zipFileName);
        Files.copy(zipFile.toPath(), response.getOutputStream());
    }
}
